﻿@using System.IO;
@model WidgetDescriptor
@{
    Layout = null;
    var tmpl = ViewBag.Template != null ? ViewBag.Template : Model.GetTemplate();
    var _json = string.IsNullOrEmpty(Model.Defaults) ? "{}" : Model.Defaults;
    var _widgetPath = Server.MapPath("~/content/widgets/") + tmpl.ID;
    var _cssPath = _widgetPath + "\\styles";
    var _scriptPath = _widgetPath + "\\scripts";
    var _stylesheets = new List<string>();
    var _scripts = new List<string>();
    if (Directory.Exists(_cssPath))
    {
        string[] stylesheetFiles = Directory.GetFiles(_cssPath, "*.css");
        for (int i = 0; i < stylesheetFiles.Length; i++)
        {
            _stylesheets.Add("~/content/widgets/" + tmpl.ID.Replace("\\", "/") + "/styles/" + Path.GetFileName(stylesheetFiles[i]));
        }
    }

    if (Directory.Exists(_scriptPath))
    {
        string[] scriptFiles = Directory.GetFiles(_scriptPath, "*.js");
        for (int i = 0; i < scriptFiles.Length; i++)
        {
            _scripts.Add("~/content/widgets/" + tmpl.ID.Replace("\\", "/") + "/scripts/" + Path.GetFileName(scriptFiles[i]));
        }
    }
}
<!DOCTYPE html>
<html>
<head>
    <title>@GE.GetContent(Model.Title)</title>
    @foreach (var cssStyleSheet in _stylesheets)
    { 
        <link rel="stylesheet" href="@Url.Content(cssStyleSheet)" type="text/css" />
    }
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.min.js")" ></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui.min.js")" ></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/modernizr.min.js")" ></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/dna/runtime.min.js")" ></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/dna/widgets.js")" ></script>
    @if (!string.IsNullOrEmpty(tmpl.Feature.Url))
    {
        var _path = tmpl.Feature.Url;
        if (_path.StartsWith("~/"))
        {
            _path = Url.Content(_path);
        }
        var _url = new UrlBuilder(_path);

        if (tmpl.Feature.Params != null && tmpl.Feature.Params.Count > 0)
        {
            foreach (var _param in tmpl.Feature.Params)
            {
                _url.AddParam(_param.Name, _param.Value.ToString());
            }
        }
        <script type="text/javascript" src="@_url.ToString()"></script>
    }
    <script type="text/javascript">
        $(function () {
            window.$widget = new __widget(@(MvcHtmlString.Create(_json)));
            var _pros= { 
               id:@(Model.ID),
               title:"@GE.GetContent(Model.Title)",
               description:"@Model.Description",
               titleLinkUrl:"@Model.TitleLinkUrl",
               imageUrl:"@Model.ImageUrl",
               iconUrl:"@Model.IconUrl"
            };
            $.extend($widget,_pros);
            //alert($widget.getUserPreferences());
        });
    </script>
    @foreach (var includeScript in _scripts)
    { 
        <script type="text/javascript" src="@Url.Content(includeScript)"></script>
    }
</head>
<body>
    @MvcHtmlString.Create(tmpl.Content.Text)
</body>
</html>
